package summit.basecamp.helpers
{
import summit.basecamp.constants.BasecampOperation;
import summit.net.BasecampRequest;
import flash.net.URLRequestMethod;
	
internal class SharedOperations
{
	public function SharedOperations()
	{
	}
	
	internal static function nullEmptyAssert(value:String, name:String, 
	nameAsMsg:Boolean = false):void
	{
		// in your compiler settings... 
		// -define+=CONFIG::debugging,true turns this on  ... ,false - off
		CONFIG::debugging 
		{
			if(null == value || "" == value || "null" == value)
			throw new ArgumentError(nameAsMsg ? name : name + 
			" can not be null or empty");
		}
	}
	
	internal static function destroy(id:String, pathHelper:String,
	type:String):BasecampRequest
	{
		nullEmptyAssert(id, "id");
		
		return new BasecampRequest(pathHelper + id + ".xml", 
		URLRequestMethod.DELETE, type, BasecampOperation.DESTROY);
	}
	
	internal static function edit(id:String, pathHelper:String, 
	type:String):BasecampRequest
	{
		nullEmptyAssert(id, "id");
		
		var request:BasecampRequest = new BasecampRequest(
		pathHelper + id + "/edit.xml", URLRequestMethod.GET, type,
		BasecampOperation.EDIT);
		
		request.xmlAdditions = new Object();
		request.xmlAdditions["id"] = id;
		
		return request;
	}
	
	internal static function read( id:String, pathHelper:String, 
	type:String):BasecampRequest
	{
		nullEmptyAssert(id, "id");
		
		return new BasecampRequest(
		pathHelper + id + ".xml", URLRequestMethod.GET, 
		type, BasecampOperation.READ);
	}
	
	internal static function prepare(path:String, 
	key:String, value:String, type:String, 
	additions:Object = null):BasecampRequest
	{
		var request:BasecampRequest = new BasecampRequest(path,
			URLRequestMethod.GET, type, BasecampOperation.PREPARE);
	
		if(!additions)
		{
			request.xmlAdditions = new Object();
			request.xmlAdditions[key] = value;
		}
		else request.xmlAdditions = additions;
		
		return request; 
	}
	
	internal static function update(content:XML, pathHelper:String,
	type:String):BasecampRequest
	{
		var id:String = content.child("id")[0];
		
		nullEmptyAssert(id, "content does not contain a valid id", true);
		
		return new BasecampRequest(
		pathHelper + id + ".xml", URLRequestMethod.PUT, type,
		BasecampOperation.UPDATE, content);
	}
}
}